/**
  * <br>JAVACC DEMO 1.0<br>
  * @copy right dwu company All rights reserved.<br>
  * <br>
  * @Package com.apache.license.filter
*/
package com.apache.license.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.apache.license.validator.LicenseValidate;
import com.apache.license.validator.OsUtils;

/**
  * description:  系统license验证类
  * <p> <filter>
  *        <filter-name>auditFilter</filter-name>
  *        <filter-class>com.apache.license.filter.AuditFilter</filter-class>
  *        <init-param>
  *            <param-name>gotoPath</param-name><!-- license出错跳转页面  -->
  *            <param-value>invalidlicense.jsp</param-value>
  *        </init-param>
  *        <init-param>
  *            <param-name>initDay</param-name><!-- 提前提示license到期的天数  -->
  *            <param-value>7</param-value>
  *        </init-param>
  *     </filter>
  *     <filter-mapping>
  *         <filter-name>auditFilter</filter-name>
  *         <url-pattern>/*</url-pattern>
  *     </filter-mapping> </P>
  * @author Hou DaYu 创建时间：上午08:53:09
  */
public class AuditFilter extends AbstractFilter {

	private String gotoPath = "invalidlicense.jsp";//license出错跳转页面
	private int alertDay = 7;//提前提示license到期的天数

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain arg2) throws IOException,
			ServletException {
		HttpServletRequest httpservletrequest = (HttpServletRequest) request;
		HttpServletResponse httpservletresponse = (HttpServletResponse) response;
		String path = httpservletrequest.getRequestURI();
		long lastTime = LicenseValidate.getInstance().isTimeByDay(alertDay);//返回要提醒的天数
		if (lastTime == -1) {
			httpservletresponse.sendRedirect(httpservletrequest.getContextPath() + "/" + gotoPath
					+ "?Errorcode=apache-00000001&dayNum=" + lastTime);//License 超过有效期！
			return;
		} else {
			if (lastTime <= alertDay) {//有效期验证
				/////////////////license将到期提醒操作////////////////////
				warnLicenseInfo(httpservletrequest, httpservletresponse, lastTime);
			}
		}

		if (LicenseValidate.getInstance().isFormal()) {//正式license号
			if (path.indexOf(gotoPath) != -1) {
				arg2.doFilter(request, response);
				return;
			}
			boolean isIp = LicenseValidate.getInstance().isip(OsUtils.getLocalIp());
			boolean isMac = LicenseValidate.getInstance().ismac(OsUtils.getMACAddress());
			if (!isIp || !isMac) {
				httpservletresponse.sendRedirect(httpservletrequest.getContextPath() + "/" + gotoPath
						+ "?Errorcode=apache-00000002");//服务器IP或mac地址未授权
				return;
			}
		}
		arg2.doFilter(request, response);
	}

	/**
	 * 提示用户license即将到期
	 */
	protected void warnLicenseInfo(HttpServletRequest request, HttpServletResponse response, long lastTime) {
		request.getSession().setAttribute("licenseTodata", lastTime);
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig filterConfig) throws ServletException {
		if (filterConfig.getInitParameter("gotoPath") != null)
			this.gotoPath = filterConfig.getInitParameter("gotoPath");
		if (filterConfig.getInitParameter("initDay") != null) {
			alertDay = Integer.parseInt(filterConfig.getInitParameter("initDay"));
		}
		this.initLincense();
	}

	/* (non-Javadoc)
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub

	}

}
